package com.example.lcode.backtrack;

import java.util.ArrayList;
import java.util.List;

public class Question39 {

    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> res = new ArrayList<>();
        combinationSum(candidates, target, res, new ArrayList<Integer>(), 0, 0);
        return res;
    }

    private <E> void combinationSum(int[] candidates, int target, List<List<Integer>> res, List<Integer> re, int startIndex, int calc) {
        if (calc >= target) {
            if (calc == target) {
                res.add(new ArrayList<>(re));
            }
            return;
        }
        for (int i = startIndex; i < candidates.length; i++) {
            re.add(candidates[i]);
            combinationSum(candidates, target, res, re, i, calc + candidates[i]);
            re.remove(re.size() - 1);
        }
    }

    public static void main(String[] args) {
        int []arr = {1};
        int target = 2;
        Question39 question39 = new Question39();
        List<List<Integer>> lists = question39.combinationSum(arr, target);
        System.out.println(lists);
    }

}
